const config = require('./config');
const scraper = require('./scraper');
const db = require('./db');
const { log } = require('./utils');

async function test() {
  try {
    // 初始化数据库
    await db.init(config.db);
    log('数据库初始化成功');
    
    // 测试爬取功能
    log('开始爬取新闻...');
    const newsList = await scraper.fetchNews(config.targetUrl);
    log(`成功爬取 ${newsList.length} 条新闻`);
    
    // 测试存储功能
    log('开始存储新闻...');
    await db.saveNews(newsList);
    log('新闻存储完成');
    
    // 显示第一条新闻信息
    if (newsList.length > 0) {
      const firstNews = newsList[0];
      log('第一条新闻信息:');
      log(`标题: ${firstNews.title}`);
      log(`发布时间: ${firstNews.publishTime}`);
      log(`图片数量: ${firstNews.images.length}`);
    }
  } catch (error) {
    log(`测试失败: ${error.message}`, 'error');
  } finally {
    await db.close();
  }
}

test();
